package com.jpaAutoProcess.utils;

import com.jpaAutoProcess.utils.type.Condition;
import lombok.Getter;
import lombok.Setter;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: c.c
 * @Date: 2022/6/7
 * @Time: 17:24
 * @Description: java对应sql字段 与 条件的解析和创建
 */
@Getter
@Setter
public class ParamConditionBO {
    /**
     * 属性名
     * @param propertyName 字段名
     */
    private String propertyName;

    /**
     * 查询条件
     * @param condition 判断条件
     */
    private Condition condition;


    public ParamConditionBO() {
    }

    /***
     * @author: C.c
     * @date: 2022-06-10 10:39
     * @description: 初始化ParamConditionBO
     * @param wherePropertyName 字段名字（一定要与数据库字段，语句对应。 如select a.name 那么wherePropertyName就为 a.name。 再如 select * 那么wherePropertyName应该为数据库存在的字段 否则可能会报数据找不到错误）
     * @param condition 条件 用枚举Condition
    **/
    public ParamConditionBO(String wherePropertyName, Condition condition) {
        propertyName = wherePropertyName.trim();
        this.propertyName = propertyName;
        this.condition = condition;
    }


    public static ParamConditionBO parse(String s) {
        ParamConditionBO columnConditionBO = new ParamConditionBO();
        String[] strings = s.split("-");
        if (strings[0] == null || strings[0].equals("")) {
            throw new RuntimeException("数据库字段为空，请检查");
        }
        if (Condition.valueOf(strings[1]) == null) {
            throw new RuntimeException("sql条件语句不正确，请检查后输入");
        }
        columnConditionBO.setPropertyName(strings[0]);
        columnConditionBO.setCondition(Condition.valueOf(strings[1]));
        return columnConditionBO;
    }

    public static String initparam(String sqlFieldname, Condition condition) {
        return new StringBuilder(sqlFieldname).append("-").append(condition).toString();
    }

}
